export default class changeData {
	static findDuplicateCardOrPhone(arr, partyCard = 'partyCard', partyPhone = 'partyPhone') {
		const seenCards = new Set();
		const seenPhones = new Set();
		for (let item of arr) {
			if (seenCards.has(item[partyCard])) {
				return item[partyCard];
			}
			if (seenPhones.has(item[partyPhone])) {
				return item[partyPhone];
			}
			seenCards.add(item[partyCard]);
			seenPhones.add(item[partyPhone]);
		}
		return null;
	}
	static phonePattern = /^(13\d|14[5-9]|15[0-35-9]|16[2567]|17[0-9]|18\d|19[0-35-9])\d{8}$/

	static idNumber = /^[a-zA-Z0-9]+$/

	static iEnglish = /^[A-Za-z]+$/
	static cardRouls(rule, value, callback) {
		if (value == '111111111111111111')return false
		const birthdateMatch = /^\d{6}(\d{4})(\d{2})(\d{2})\d{3}[0-9Xx]$/.exec(value);
		if (!birthdateMatch) return false;
		const [_, yearStr, monthStr, dayStr] = birthdateMatch;
		const birthYear = parseInt(yearStr, 10);
		const birthMonth = parseInt(monthStr, 10);
		const birthDay = parseInt(dayStr, 10);
		const birthDate = new Date(birthYear, birthMonth - 1, birthDay);
		const now = new Date();
		const nowYear = now.getFullYear();
		const nowMonth = now.getMonth() + 1;
		const nowDay = now.getDate();
		if (birthDate.getFullYear() !== birthYear || birthDate.getMonth() + 1 !== birthMonth ||
			birthDate.getDate() !== birthDay) return false;
		let age = nowYear - birthYear;
		if (nowMonth < birthMonth || (nowMonth === birthMonth && nowDay < birthDay)) {
			age--;
		}
		return (age >= 0);
	}
	static changeTreeData(list) {
		return list.map(({
			id,
			areaName,
			children,
			parentId
		}) => {
			children = children ? this.changeTreeData(children) : []
			return {
				id,
				areaName,
				children,
				parentId
			};
		})
	}
	static findArea(areas, path){
		let currentArea = unref(areas);
		let id
		for (let i = 0; i < path.length; i++) {
			const areaName = path[i];
			const arr = currentArea.find(area => area.areaName === areaName)
			currentArea = arr.children;
			id = arr.id
		}
		return id
	}
}